package slu.appli.tools.parser.stringparser.impl.treemodel;

import java.util.ArrayList;
import java.util.List;

import slu.appli.tools.parser.stringparser.impl.model.AbstractModelBuilder;
import slu.appli.tools.parser.stringparser.interfaces.model.GroupInterface;

public class BasicTreeModelBuilder
extends AbstractModelBuilder {
	private List<BasicTreeNode> parents = new ArrayList<BasicTreeNode>();
	private Object dataContext = null;

	public BasicTreeModelBuilder(BasicTreeNode node) {
		super();
		this.parents.add(node);
	}

	public void addItem(Object item) {
		this.parents.get(this.getLevel()).addChild(item);
	}

	@Override
	public void closeGroup() {
		super.closeGroup();
	}

	@Override
	public void openGroup(int occurs) {
		super.openGroup(occurs);
		BasicTreeNode node = new BasicTreeNode(occurs);
		this.parents.get(this.getLevel()-1).addChild(node);
		if (this.getLevel() < this.parents.size())
			this.parents.set(this.getLevel(), node);
		else
			this.parents.add(node);
	}

	public GroupInterface getRoot() {
		return this.parents.get(0);
	}

	public void clear() {
		this.parents.clear();
		this.parents.add(new BasicTreeNode());
	}

	public Object getDataContext() {
		return this.dataContext;
	}

	public void setDataContext(Object dataContext) {
		this.dataContext = dataContext;
	}
}
